<{include file="public/header" /}>

<?php

    use Tools\AdminPermission;

    $role_id = Session::get('role_id');
    $adminPermission = new AdminPermission();
    $adminPermission->setRoleId($role_id);
    $module_name = 'activity';
?>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <textarea id="list_filters_box" style="display: none"></textarea>
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">搜索内容</label>
                            <div class="layui-input-inline" style="min-width: 240px">
                                <input type="text" name="filters[search_text]" autocomplete="off" class="layui-input" placeholder="活动ID、活动标题、商家ID">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">所属分类</label>
                            <div class="layui-input-inline" style="min-width: 100px">
                                <select id="filters-category_id" name="filters[category_id]">
                                    <option value="0">全部</option>
                                    <{volist name="categorys" id= "category"}>
                                        <option value="<{$category.id}>"><{$category.title}></option>
                                    <{/volist}>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">活动状态</label>
                            <div class="layui-input-inline" style="min-width: 100px">
                                <select id="filters-status" name="filters[status]">
                                    <option value="0">全部</option>
                                    <option value="1">待支付</option>
                                    <option value="2">待审核</option>
                                    <option value="3">审核驳回</option>
                                    <option value="4">已结束</option>
                                    <option value="100">进行中</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <!--<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>-->
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail">详情</a>

            <{if checkAdminPermissionByAction($adminPermission,$module_name,'audit')}>
                {{#  if(d.status == 2){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="audit">审核</a>
                {{#  } else if(d.status == 1) { }}
                    <a class="layui-btn layui-btn-xs layui-btn-disabled" onclick="error_msg('不可审核，待支付')">审核</a>
                {{#  } else { }}
                    <a class="layui-btn layui-btn-xs layui-btn-disabled" onclick="error_msg('不可审核，已审核')">审核</a>
                {{#  } }}
            <{/if}>
            <!--
            {{#  if(d.status == 100){ }}
                <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="stop">下架</a>
            {{#  } else { }}
                <a class="layui-btn layui-btn-xs layui-btn-disabled" onclick="error_msg('不可下架，未上架')">下架</a>
            {{#  } }}
            -->
        </script>

        <script type="text/html" id="categoryTitleTextTpl">
            {{d.category.title}}
        </script>

        <script type="text/html" id="merchantTextTpl">
            {{d.merchant.id}} : {{d.merchant.nick_name}}
        </script>

        <script type="text/html" id="stotalTextTpl">
            {{d.total - (d.apply_total - d.cancel_total)}}
        </script>

        <script type="text/html" id="statusTextTpl">
            {{#  if(d.status == 1){ }}
            待支付
            {{#  }else if(d.status == 2){ }}
            待审核
            {{#  }else if(d.status == 3){ }}
            审核驳回
            {{#  }else if(d.status == 4){ }}
            已结束
            {{#  }else if(d.status == 100){ }}
            进行中
            {{#  }else{ }}
            未知
            {{#  } }}
        </script>
    </div>
</div>

<script>

    layui.use(['form', 'table', 'upload', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            upload = layui.upload,
            table = layui.table,
            laydate = layui.laydate;

        table.render({
            elem: '#currentTableId',
            url: "<{:admin_api_url('activity/list')}>"
                <{include file="public/table_common" /}>,
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'category_title',minWidth: 100, title: '所属分类', templet:"#categoryTitleTextTpl"},
                {field: 'title', title: '活动标题',minWidth: 200},
                {field: 'project_title', title: '项目标题',hide:true},
                {field: 'merchant', title: '商家ID:昵称', templet:"#merchantTextTpl"},
                {field: 'price', title: '单价'},
                {field: 'total', title: '总量'},
                {field: 'stotal', title: '剩余', templet:"#stotalTextTpl"},
                {field: 'created_at', width: 200, title: '发布时间', sort: true, hide: true},
                {field: 'status', width: 110, title: '状态', templet:"#statusTextTpl", sort: true},
                {field: 'tool',title: '操作', minWidth: 120, fixed:'right',templet:'#currentTableBar', align: "center"}
            ]]
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {

        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            set_list_filters(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {

                let url = "<{:admin_api_url('activity/detail')}>";
                $.post(url,{'_method':'get', id: data.id},function(res){
                    ajax_response_filtr(res,$adminConfig);
                    if(res.status == 200){
                        var index = layer.open({
                            title: '广告详情',
                            type: 1,
                            shade: 0.2,
                            shadeClose: true,
                            area: ['95%', '95%'],
                            content: res.data,
                            success: function(layero, index){

                                $(window).on("resize", function () {
                                    layer.full(index);
                                });

                                form.render();
                            }
                        });
                    }
                },'json');

                return false;
            }else if (obj.event === 'audit') {

                let url = "<{:admin_api_url('activity/audit')}>";
                $.post(url,{'_method':'get', id: data.id},function(res){
                    ajax_response_filtr(res,$adminConfig);
                    if(res.status == 200){
                        var index = layer.open({
                            title: '活动审核',
                            type: 1,
                            shade: 0.2,
                            shadeClose: true,
                            area: ['90%', '90%'],
                            btn: ['确定', '取消'],
                            content: res.data,
                            success: function(layero, index){

                                form.on('radio(status)', function(data){
                                    if(data.value == '100'){
                                        $('.reasons_box').hide();
                                    }else{
                                        $('.reasons_box').show();
                                    }
                                });
                                form.render();
                            },
                            yes: function(index, layero){

                                let submitID = 'LAY-activity-audit-submit';
                                let submit = layero.find('#'+ submitID);

                                //监听提交
                                form.on('submit('+ submitID +')', function(data){

                                    var field = data.field; //获取提交的字段
                                    field.status = field.status == 100 ? 100 : 3;

                                    //通过
                                    if(field.status != '100'){
                                        if(!field.reasons){
                                            error_msg('备注必填');return;
                                        }
                                    }else{
                                        field.reasons = '';
                                    }

                                    //提交
                                    $.post(url,field,function(res){
                                        ajax_response_filtr(res,$adminConfig);
                                        if(res.status == 200){
                                            obj.update({status: field.status,tool:true});
                                            layer.close(index); //关闭弹层
                                            success_msg('操作成功');
                                        }

                                    },'json')

                                });

                                submit.trigger('click');
                            },
                        });
                    }
                },'json');

                return false;
            }else if (obj.event === 'delete') {

                layer.confirm('您真的要删除这个广告吗？', function (index) {
                    layer.close(index);
                    let url = "<{:admin_api_url('activity/delete')}>";
                    $.post(url,{id: data.id},function(res){
                        ajax_response_filtr(res,$adminConfig);
                        if(res.status == 200){
                            obj.del();
                            success_msg('操作成功');
                        }
                    },'json');
                });
                return false;
            }
        });

    });


    function delete_content_row(index) {
        $('.material-list .material-row[data-content-index="'+index+'"]').remove();
    }
</script>

<{include file="public/footer" /}>